package com.da.javatest.design.schema.prototype;

public class Sheep2 implements Cloneable {
    private String name;
    private int age;
    private String color;
    private String address = "蒙古羊";
    public Sheep2 friend; //是对象, 克隆是会如何处理

    public Sheep2(String name, int age, String color) {
        super();
        this.name = name;
        this.age = age;
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Sheep2 [name=" + name + ", age=" + age + ", color=" + color + ", address=" + address + "]";
    }

    //克隆该实例，使用默认的clone方法来完成
    @Override
    protected Object clone() {

        Sheep2 sheep = null;
        try {
            sheep = (Sheep2)super.clone();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sheep;
    }

    public static void main(String[] args) {
        System.out.println("原型模式完成对象的创建");
        Sheep2 sheep = new Sheep2("tom", 1, "白色");

        sheep.friend = new Sheep2("jack", 2, "黑色");

        Sheep2 sheep2 = (Sheep2)sheep.clone(); //克隆
        Sheep2 sheep3 = (Sheep2)sheep.clone(); //克隆
        Sheep2 sheep4 = (Sheep2)sheep.clone(); //克隆
        Sheep2 sheep5 = (Sheep2)sheep.clone(); //克隆

        System.out.println("sheep2 =" + sheep2 + "sheep2.friend=" + sheep2.friend.hashCode());
        System.out.println("sheep3 =" + sheep3 + "sheep3.friend=" + sheep3.friend.hashCode());
        System.out.println("sheep4 =" + sheep4 + "sheep4.friend=" + sheep4.friend.hashCode());
        System.out.println("sheep5 =" + sheep5 + "sheep5.friend=" + sheep5.friend.hashCode());
    }

}